package com.tz.yiqingapp.controller.doctor;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tz.yiqingapp.bean.Hosptial;
import com.tz.yiqingapp.bean.User;
import com.tz.yiqingapp.bean.UserHealth;
import com.tz.yiqingapp.bean.UserHealthReport;
import com.tz.yiqingapp.config.AccessLimit;
import com.tz.yiqingapp.dto.PageDTO;
import com.tz.yiqingapp.mapper.*;
import com.tz.yiqingapp.services.dao.CommandServiceImpl;
import com.tz.yiqingapp.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/doctor")
public class DoctorHealthCheck {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    HosptialMapper hosptialMapper;

    @Autowired
    DoctorMapper doctorMapper;

    @Autowired
    CommandServiceImpl commandService;

    @Autowired
    UserMapper userMapper;
    @Autowired
    UserHealthReportMapper userHealthReportMapper;


    @Autowired
    UserHealthMapper userHealthMapper;


    @AccessLimit(times = 5)
    @PostMapping("/checkHealth")
    @ResponseBody
    public HashMap<String, Object> checkHealth(@RequestBody PageDTO pageDTO, HttpServletRequest request) {

        List<Hosptial> hosptials = hosptialMapper.selectList(null);
        return ResponseUtils.success(hosptials);
    }


    @AccessLimit(times = 5)
    @PostMapping("/getUnCheckUser")
    @ResponseBody
    public HashMap<String, Object> getUnCheckUser(@RequestBody PageDTO pageDTO, HttpServletRequest request) {
        if (request.getSession().getAttribute("loginrole") != null && request.getSession().getAttribute("loginrole").equals("doctor")) {
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.lambda().eq(User::getChecked, false).and(userLambdaQueryWrapper -> userLambdaQueryWrapper.eq(User::getInfoed,true));
            List<User> users = userMapper.selectList(userQueryWrapper);
            if (users.size() > 0)
                return ResponseUtils.success(users.get(0));
            return ResponseUtils.fail("无待检测用户！");
        }
        return ResponseUtils.notlogin("未登录！");
    }
    @AccessLimit(times = 5)
    @PostMapping("/reportUserHealth")
    @ResponseBody
    public HashMap<String, Object> reportUserHealth(@RequestBody UserHealth userHealth, HttpServletRequest request) {
        if (request.getSession().getAttribute("loginrole") != null && request.getSession().getAttribute("loginrole").equals("doctor")) {

            QueryWrapper<UserHealth> userHealthQueryWrapper = new QueryWrapper<>();
            userHealthQueryWrapper.lambda().eq(UserHealth::getUid,userHealth.getUid());
            List<UserHealth> userHealths = userHealthMapper.selectList(userHealthQueryWrapper);
            if(userHealths.size()>0){
                return ResponseUtils.fail("该用户已存在身体检查信息！");
            }
            User user = userMapper.selectById(userHealth.getUid());
            if(user!=null) {
                user.setChecked(true);
                userMapper.updateById(user);
            }
            userHealthMapper.insert(userHealth);
            String rbc = commandService.executeCmd("cd predict ; python3 dtree_disase.py rbc " + userHealth.getRbc()).trim();
            String hb = commandService.executeCmd("cd predict ; python3 dtree_disase.py hb " + userHealth.getHb()).trim();
            String plt = commandService.executeCmd("cd predict ; python3 dtree_disase.py plt " + userHealth.getPlt()).trim();
            String ly = commandService.executeCmd("cd predict ; python3 dtree_disase.py ly " + userHealth.getLy()).trim();
            String mono = commandService.executeCmd("cd predict ; python3 dtree_disase.py mono " + userHealth.getMono()).trim();
            String zam = commandService.executeCmd("cd predict ; python3 dtree_disase.py zam " + userHealth.getZam()).trim();
            String apky = commandService.executeCmd("cd predict ; python3 dtree_disase.py apky " + userHealth.getApky()).trim();
            String ns = commandService.executeCmd("cd predict ; python3 dtree_disase.py ns " + userHealth.getNs()).trim();
            String xt = commandService.executeCmd("cd predict ; python3 dtree_disase.py xt " + userHealth.getXt()).trim();
            String xy = commandService.executeCmd("cd predict ; python3 dtree_disase.py xy " + userHealth.getXy()).trim();
            UserHealthReport userHealthReport = new UserHealthReport();
            userHealthReport.setUid(userHealth.getUid());
            userHealthReport.setApky(apky.equals("1"));
            userHealthReport.setHb( rbc.equals("1") ||hb.equals("1"));
            userHealthReport.setLy(ly.equals("1"));
            userHealthReport.setMono(mono.equals("1"));
            userHealthReport.setZam(zam.equals("1"));
            userHealthReport.setNs(ns.equals("1"));
            userHealthReport.setXt(xt.equals("1"));
            userHealthReport.setXy(xy.equals("1"));
            userHealthReportMapper.insert(userHealthReport);
            return ResponseUtils.success(userHealthReport);
        }
        return ResponseUtils.notlogin("未登录！");
    }
}
